package com.erp.order.purchasing.dao.impl;

import com.erp.hr.base.dao.impl.BaseDaoImpl;
import com.erp.hr.base.util.CommonUtils;
import com.erp.order.purchasing.dao.PurchasingRequisitionDao;
import com.erp.order.purchasing.model.PurchasingRequisition;
import org.apache.commons.lang.StringUtils;
import org.redragon.framework.annotation.Permissions;
import org.redragon.framework.annotation.Permissions.PermissionType;
import org.redragon.framework.annotation.SqlParam;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.DaoCRUDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 采购申请 持久实现层
 * @Author: syz
 * @blame syz
 * @CreateTime: 2021/4/6
 */
@Repository
public class PurchasingRequisitionDaoImpl extends BaseDaoImpl<PurchasingRequisition> implements PurchasingRequisitionDao {

    /**
     * dao支持
     */
    @Autowired
    private DaoSupport daoSupport;


    /**
     * 获取列表分页数据
     * @param dataAuthSQL 数据验证sql
     * @param pages 分页对象
     * @param paramObj 查询条件对象
     * @return 列表分页数据
     */
    @Override
    @Permissions(PermissionType.DATA_AUTH)
    public List<PurchasingRequisition> getDataObjectsForDataAuth(@SqlParam String dataAuthSQL, Pages pages, PurchasingRequisition paramObj) {
        String sql =
                "SELECT " +
                    "pr.* " +
                "FROM " +
                    "purchasing_requisition pr " +
                "WHERE " +
                    "pr.create_user = :user ";
        Map<String, Object> args = new HashMap<>();
        args.put("user", paramObj.getCreateUser());
        if (StringUtils.isNotBlank(paramObj.getCode())) {
            paramObj.setCode("%" + paramObj.getCode() + "%");
            sql += DaoCRUDUtil.getSQLLikeCondition(paramObj, "code", "and pr.", args);
        }
        if (StringUtils.isNotBlank(paramObj.getTitle())) {
            paramObj.setTitle("%" + paramObj.getTitle() + "%");
            sql += DaoCRUDUtil.getSQLLikeCondition(paramObj, "title", "and pr.", args);
        }
        if (StringUtils.isNotBlank(paramObj.getCompanyCode())) {
            paramObj.setCompanyCode("%" + paramObj.getCompanyCode() + "%");
            args.put("companycode", paramObj.getCompanyCode());
            sql += "AND " +
                        "CONCAT(pr.company_code, ' - ', pr.company_name) LIKE :companycode ";
        }
        if (StringUtils.isNotBlank(paramObj.getDepartmentName())) {
            paramObj.setDepartmentName("%" + paramObj.getDepartmentName() + "%");
            sql += DaoCRUDUtil.getSQLLikeCondition(paramObj, "departmentName", "and pr.", args);
        }
        sql += DaoCRUDUtil.getSQLSearch(paramObj, "type", "and pr.", args);
        sql += DaoCRUDUtil.getSQLSearch(paramObj, "status", "and pr.", args);
        sql +=
                "AND " +
                    "pr.is_del = '0' " +
                "ORDER BY " +
                    "pr.update_time DESC";
        daoSupport.validateDataObject(paramObj);
        return daoSupport.getDataSqlByPage(
                sql,
                new HashMap<String, Class<?>>(){{
                    put("pr", PurchasingRequisition.class);
                }},
                args,
                pages);
    }

    /**
     * 根据id查询对象
     * @param id 对象id
     * @return 查询结果
     */
    @Override
    public PurchasingRequisition getDataObject(String id) {
        String sql =
                "SELECT " +
                    "pr.* " +
                "FROM " +
                    "purchasing_requisition pr " +
                "WHERE " +
                    "pr.id = :id " +
                "AND " +
                    "pr.is_del = :isDel";
        Map<String, Object> args = new HashMap<>();
        args.put("id", id);
        args.put("isDel", CommonUtils.IS_DEL_NO);
        Map<String, Class<?>> entity = new HashMap<>();
        entity.put("pr", PurchasingRequisition.class);
        List<PurchasingRequisition> list = daoSupport.selectDataSql(sql, entity, args);
        return list != null && list.size() > 0 ? list.get(0) : null;
    }

    /**
     * 根据条件查询对象集合
     * @param purchasingRequisition 采购申请对象
     * @return 对象集合
     */
    @Override
    public List<PurchasingRequisition> getDataObjects(PurchasingRequisition purchasingRequisition) {
        return daoSupport.selectDataSql(
                "SELECT " +
                        "pr.* " +
                    "FROM " +
                        "purchasing_requisition pr " +
                    "WHERE " +
                        "pr.id IN (:ids) " +
                    "AND " +
                        "pr.is_del = :isDel",
                new HashMap<String, Class<?>>(){{
                    put("pr", PurchasingRequisition.class);
                }},
                new HashMap<String, Object>(){{
                    put("ids", purchasingRequisition.getIds());
                    put("isDel", CommonUtils.IS_DEL_NO);
                }});
    }

    /**
     * 根据计划id获取详情集合
     * @param id 计划id
     * @return 详情集合
     */
    @Override
    public List<PurchasingRequisition> getListByPurchasingId(Long id) {
        return daoSupport.selectDataSql(
                "SELECT " +
                        "pr.* " +
                    "FROM " +
                        "purchasing_requisition pr " +
                    "WHERE " +
                        "pr.purchasing_id = :id " +
                    "AND " +
                        "pr.is_del = :isDel",
                new HashMap<String, Class<?>>(){{
                    put("pr", PurchasingRequisition.class);
                }},
                new HashMap<String, Object>(){{
                    put("id", id);
                    put("isDel", CommonUtils.IS_DEL_NO);
                }});
    }

    /**
     * 根据申请类型获取时间范围内的申请数量
     * @param type 申请类型
     * @return 时间范围内的申请数量
     */
    @Override
    public Integer getRequisitionCount(String type) {
        String sql =
                "SELECT " +
                    "pr.* " +
                "FROM " +
                    "purchasing_requisition pr " +
                "WHERE " +
                    "pr.type = :type " +
                "AND " +
                    "pr.is_del = :isDel ";
        if (CommonUtils.PURCHASING_REQUISITION_TYPE_SPORADIC.equals(type)) {
            sql +=
                    "AND " +
                        "DATE_FORMAT(pr.create_time, '%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d')";
        } else {
            sql +=
                    "AND " +
                        "DATE_FORMAT(pr.create_time, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%m')";
        }
        Map<String, Object> args = new HashMap<>();
        args.put("type", type);
        args.put("isDel", CommonUtils.IS_DEL_NO);
        Map<String, Class<?>> entity = new HashMap<>();
        entity.put("pr", PurchasingRequisition.class);
        List<PurchasingRequisition> result = daoSupport.selectDataSql(sql, entity, args);
        return result == null ? 0 : result.size();
    }

    /**
     * 判断部门当月是否已提交月采购申请
     * @param staffCodes 部门下员工编码集合
     * @return 部门当月是否已提交月采购申请
     */
    @Override
    public boolean isResubmit(List<String> staffCodes) {
        String sql =
                "SELECT " +
                    "pr.* " +
                "FROM " +
                    "purchasing_requisition pr " +
                "WHERE " +
                    "pr.type = :type " +
                "AND " +
                    "pr.create_user IN (:staffCodes) " +
                "AND " +
                    "DATE_FORMAT(pr.create_time, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%m') " +
                "AND " +
                    "pr.status = :status " +
                "AND " +
                    "pr.is_del = :isDel";
        Map<String, Object> args = new HashMap<>();
        args.put("type", CommonUtils.PURCHASING_REQUISITION_TYPE_MONTH);
        args.put("staffCodes", staffCodes);
        args.put("status", CommonUtils.PURCHASING_REQUISITION_STATUS_SUBMIT);
        args.put("isDel", CommonUtils.IS_DEL_NO);
        Map<String, Class<?>> entity = new HashMap<>();
        entity.put("pr", PurchasingRequisition.class);
        List<PurchasingRequisition> result = daoSupport.selectDataSql(sql, entity, args);
        return result != null && result.size() > 0;
    }

    /**
     * 根据id集合获取采购申请集合
     * @param ids id集合
     * @return 采购申请集合
     */
    @Override
    public List<PurchasingRequisition> getListByIds(Set<Long> ids) {
        return daoSupport.selectDataSql(
                "SELECT " +
                        "pr.* " +
                    "FROM " +
                        "purchasing_requisition pr " +
                    "WHERE " +
                        "pr.id IN (:ids) " +
                    "AND " +
                        "pr.is_del = :isDel",
                new HashMap<String, Class<?>>(){{
                    put("pr", PurchasingRequisition.class);
                }},
                new HashMap<String, Object>(){{
                    put("ids", ids);
                    put("isDel", CommonUtils.IS_DEL_NO);
                }});
    }

}